package com.nmsl.statistic.job;

import com.nmsl.statistic.domain.DailyFlow;
import com.nmsl.statistic.mapper.DailyFlowMapper;
import com.nmsl.statistic.utils.TimeUtil;
import org.checkerframework.checker.initialization.qual.Initialized;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Component("testTask")
public class TestJob  implements InitializingBean {


    @Autowired
    private  DailyFlowMapper dailyFlowMapper;



//    添加每日流水

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("初始化结束了afterPropertiesSet你来了吗？？？？");
        System.out.println(this.dailyFlowMapper);
        System.out.println(dailyFlowMapper);
        System.out.println("检查今日流水是否存在");
        DailyFlow dailyFlowByTime = dailyFlowMapper.getDailyFlowByTime(TimeUtil.getcruntTime("YYYY-MM-dd"));

        if (dailyFlowByTime == null) {
            System.out.println("创建今日流水");
            Long time = System.currentTimeMillis();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String nowMs = format.format(new Date(time));
//        一天后事件
            Date parse = null;
            try {
                parse = format.parse(nowMs);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            DailyFlow dailyFlow = new DailyFlow();
            dailyFlow.setDailyFlowId(UUID.randomUUID().toString().replace("-", ""));
            dailyFlow.setAmount(new BigDecimal("0"));
            dailyFlow.setCardAmount(new BigDecimal("0"));
            dailyFlow.setRefund(new BigDecimal("0"));
            dailyFlow.setDiscountAmount("0");
            dailyFlow.setPlayAmount(new BigDecimal("0"));
            dailyFlow.setShoudAmount(new BigDecimal("0"));
            dailyFlow.setCreateTime(parse);
            dailyFlowMapper.insertDailyFlow(dailyFlow);
            System.out.println("今日流水创建成功");
        }
    }
    public void createDaily() throws ParseException {
        Long time = System.currentTimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowMs = format.format(new Date(time + TimeUnit.HOURS.toMillis(24)));
//        一天后事件
        Date parse = format.parse(nowMs);
        DailyFlow dailyFlow = new DailyFlow();
        dailyFlow.setDailyFlowId(UUID.randomUUID().toString().replace("-", ""));
        dailyFlow.setAmount(new BigDecimal("0"));
        dailyFlow.setCardAmount(new BigDecimal("0"));
        dailyFlow.setRefund(new BigDecimal("0"));
        dailyFlow.setDiscountAmount("0");
        dailyFlow.setPlayAmount(new BigDecimal("0"));
        dailyFlow.setShoudAmount(new BigDecimal("0"));
        dailyFlow.setCreateTime(parse);
        dailyFlowMapper.insertDailyFlow(dailyFlow);
    }
}
